# -*- coding: utf-8 -*-
# -----------------------------------------
# @Time       : 2018/6/18 9:31
# @Author     : dqz 
# @Email      : 1406990390@qq.com
# @File       : sh_tools.py
# @Software   : PyCharm
# -----------------------------------------


import os

from utils.decorator import check_conn, check_table_key, record_api_log

# 同步表结构
from utils.log import log

@record_api_log
@check_table_key
@check_conn
def sync_schema(src, dest):
    infos = {
        "status": False,
        "msg": "同步表结构成功"
    }
    param = '--single-transaction --skip-tz-utc --set-gtid-purged=OFF --no-data'
    cmd = f'mysqldump -h{src["host"]} -P{src["port"]} -u{src["username"]} -p{src["password"]} {param} --databases {src["database"]} --tables {src["table"]} 2>/dev/null | mysql -h{dest["host"]} -P{dest["port"]} -u{dest["username"]} -p{dest["password"]} {dest["database"]} 2>/dev/null'
    log.info(cmd)
    if os.system(cmd) == 0:
        infos["status"] = True
    else:
        infos["msg"] = f"同步表结构失败：{src.get('database')}.{src.get('table')}"
    return infos


# 同步表数据
@record_api_log
@check_table_key
@check_conn
def sync_data(src, dest):
    infos = {
        "status": False,
        "msg": "同步表数据成功"
    }
    param = '--no-create-db --no-create-info --single-transaction --skip-tz-utc --set-gtid-purged=OFF'
    cmd = f'mysqldump -h{src["host"]} -P{src["port"]} -u{src["username"]} -p{src["password"]} {param} --databases {src["database"]} --tables {src["table"]} 2>/dev/null | mysql -h{dest["host"]} -P{dest["port"]} -u{dest["username"]} -p{dest["password"]} {dest["database"]} 2>/dev/null'
    log.info(cmd)
    if os.system(cmd) == 0:
        infos["status"] = True
    else:
        infos["msg"] = f"同步表数据失败：{src.get('database')}.{src.get('table')}"
    return infos


# 根据条件同步
@record_api_log
@check_table_key
@check_conn
def sync_data_by_where(src, dest, where):
    infos = {
        "status": False,
        "msg": "根据条件同步表数据成功"
    }
    where = where.replace('"', "'")
    param = '--no-create-db --no-create-info --single-transaction --skip-tz-utc --set-gtid-purged=OFF'
    cmd = f'''mysqldump -h{src["host"]} -P{src["port"]} -u{src["username"]} -p{src["password"]} {param} --databases {src["database"]} --tables {src["table"]} --where="{where}" 2>/dev/null | mysql -h{dest["host"]} -P{dest["port"]} -u{dest["username"]} -p{dest["password"]} {dest["database"]} 2>/dev/null'''
    log.info(cmd)
    if os.system(cmd) == 0:
        infos["status"] = True
    else:
        infos["msg"] = f"根据条件同步表数据失败：{src.get('database')}.{src.get('table')} where:{where}"

    return infos


# 同步表结构和数据
@record_api_log
@check_table_key
@check_conn
def sync_data_schema(src, dest):
    infos = {
        "status": False,
        "msg": "同步表结构和数据成功"
    }
    param = '--single-transaction --skip-tz-utc --set-gtid-purged=OFF'
    cmd = f'mysqldump -h{src["host"]} -P{src["port"]} -u{src["username"]} -p{src["password"]} {param} --databases {src["database"]} --tables {src["table"]} 2>/dev/null | mysql -h{dest["host"]} -P{dest["port"]} -u{dest["username"]} -p{dest["password"]} {dest["database"]} 2>/dev/null'
    log.info(cmd)
    if os.system(cmd) == 0:
        infos["statuys"] = True
    else:
        infos["msg"] = f"同步表结构和数据失败: {src.get('database')}.{src.get('table')}"
    return infos


# 根据条件同步表结构和数据
@record_api_log
@check_table_key
@check_conn
def sync_data_schema_by_where(src, dest, where):
    infos = {
        "status": False,
        "msg": "根据条件同步表结构和数据成功"
    }
    where = where.replace('"', "'")
    param = '--single-transaction --skip-tz-utc --set-gtid-purged=OFF'
    cmd = f'''mysqldump -h{src["host"]} -P{src["port"]} -u{src["username"]} -p{src["password"]} {param} --databases {src["database"]} --tables {src["table"]} --where="{where}" 2>/dev/null | mysql -h{dest["host"]} -P{dest["port"]} -u{dest["username"]} -p{dest["password"]} {dest["database"]} 2>/dev/null'''
    log.info(cmd)
    if os.system(cmd) == 0:
        infos["status"] = True
    else:
        infos["msg"] = f"根据条件同步表结构和数据成功: {src.get('database')}.{src.get('table')} where:{where}"
    return infos
